home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / SDKs / Word Services SDK 1.0.6 / Writeswell Jr 1.2.1 Sources ƒ / Writeswell Jr. Source / TestBed.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-16  |  14.6 KB  |  743 lines  |  [TEXT/KAHL]

  1. /* TestBed.c
  2.  * Word Services Testbed - main module for Writeswell Jr.
  3.  *
  4.  * ©1992 Working Software, Inc.
  5.  * This source code is copyrighted.  Permission is granted to use the Word Services
  6.  * portion of the Writeswell Jr. source code in your own programs, but you 
  7.  * may not distribute the Writeswell Jr. word-processor code as a 
  8.  * commercial product.  If you modify the code, please do not call it 
  9.  * Writeswell Jr. (or Writeswell.)  This will ensure that people understand the 
  10.  * program and don’t have to deal with a number of different versions with 
  11.  * who-knows-what going on in the code.
  12.  * 
  13.  * Writeswell Jr. and Writeswell are trademarks of Working Software, Inc.
  14. */
  15.  
  16. #include <EPPC.h>
  17. #include <AppleEvents.h>
  18. #include <AEObjects.h>
  19. #include <Printing.h>
  20. #include "AERegistry.h"
  21. #include "WordServices.h"
  22. #include "TestBed.h"
  23. #include "TBConstants.h"
  24. #include "MyFiles.h"
  25. #include "GenHandlers.h"
  26. #include "AppEvents.h"
  27. #include "AEObj.h"
  28. #include "InitMenu.h"
  29. #include "Gripe.h"
  30. #include "Scroll.h"
  31. #include "Prefs.h"
  32. #include "DoChecking.h"
  33. #include "ServiceMgr.h"
  34. #include "ObWind.h"
  35. #include "ObText.h"
  36. #include "ObNull.h"
  37. #include "FontMenu.h"
  38. #include "Options.h"
  39. #include "ServiceDialog.h"
  40. #include "PrintWWJr.h"
  41. #include "UnloadStuff.h"
  42. #include "InitWWJr.h"
  43. #include "MyGestalt.h"
  44. #include "PageSetup.h"
  45. #include "TTPictures.h"
  46. #include "WWJrAbout.h"
  47. #include "MyTextEdit.h"
  48. #include "RegMenu.h"
  49.  
  50. #define GLOBALS_HERE
  51. #include "TBGlobals.h"
  52. #undef GLOBALS_HERE
  53.  
  54. Boolean DoMouseDown( EventRecord *eventPtr );
  55. Boolean DoMenuCommand( long menuSpot );
  56. Boolean DoAppleMenu( short theItem );
  57. Boolean DoFileMenu( short theItem );
  58. Boolean DoEditMenu( short theItem );
  59. Boolean DoServMenu( short theItem );
  60. Boolean DoKeyDown( long message, short modifiers );
  61. Boolean DoActivateEvt( short modifiers );
  62. Boolean DoUpdateEvt( long message );
  63. Boolean DoOSEvt( EventRecord *eventPtr );
  64. void GrowDocWindow( WindowPtr theWindow, Point where );
  65. void SetTESize( WindowPtr theWindow );
  66. void MyDrawGrowIcon( WindowPtr theWindow );
  67. void DoQuit( void );
  68. void DoZoom( WindowPtr theWindow, short deskPart );
  69. void ResetWindowParts( WindowPtr theWindow );
  70.  
  71. void main( void )
  72. {
  73.     OSErr    err;
  74.  
  75.     MaxApplZone();
  76.     //InitGraf(&thePort);
  77.     InitGraf(&qd.thePort);
  78.     InitFonts();
  79.     /* FlushEvents(everyEvent, 0); */
  80.     InitWindows();
  81.     TEInit();
  82.     InitDialogs(0L);
  83.     
  84.     gDocDirty = false;
  85.  
  86.     gAppFileRefNum = CurResFile();
  87.  
  88.     gQuitRequested = false;
  89.     gPrintRequested = false;
  90.  
  91.     if ( !OpenPrefFile() ){
  92.         Gripe( "\pCannot open Writeswell, Jr. Preferences" );
  93.         ExitToShell();
  94.     }
  95.  
  96.     InitCursor();
  97.  
  98.     if ( System7Installed() ){
  99.         err = InitForSystem7();
  100.     }else{
  101. #ifndef GENERATINGCFM
  102.         err = InitForSystem6();
  103. #endif
  104.     }
  105.  
  106.     if ( err )
  107.         ExitToShell();
  108.  
  109.     while ( EventLoop() )
  110.         ;
  111.  
  112.     TearDown();                // Destroy routine descriptors, etc.
  113.  
  114.     ExitToShell();
  115. }
  116.  
  117. Boolean EventLoop( void )
  118. {
  119.     EventRecord theEvent;
  120.     
  121.     UnloadStuff();
  122.  
  123.     if ( gPrintRequested ){
  124.         gPrintRequested = false;
  125.         DoPrint();
  126.     }
  127.  
  128.     if ( gQuitRequested ){
  129.         DoQuit();
  130.         return false;            /* We should never reach this statement */
  131.     }
  132.  
  133.     WaitNextEvent( everyEvent, &theEvent, 30, (RgnHandle)NULL );
  134.     
  135.  
  136.     if ( gDocWindow )
  137.         TEIdle( (TEHandle)GetWRefCon( gDocWindow ) );
  138.     
  139.     switch( theEvent.what ){
  140.         case nullEvent:
  141.             DoNullEvent( &theEvent );
  142.             break;
  143.         case mouseDown:
  144.             return DoMouseDown( &theEvent );
  145.             break;
  146.         case mouseUp:
  147.             DoMouseUp();
  148.             break;
  149.         case autoKey:
  150.         case keyDown:
  151.             return DoKeyDown( theEvent.message, theEvent.modifiers );
  152.             break;
  153.         case updateEvt:
  154.             DoUpdateEvt( theEvent.message );
  155.             break;
  156.         case diskEvt:
  157.             DoDiskEvt();
  158.             break;
  159.         case activateEvt:
  160.             DoActivateEvt( theEvent.modifiers );
  161.             break;
  162.         case osEvt:
  163.             DoOSEvt( &theEvent );
  164.             break;
  165.         case kHighLevelEvent:
  166.             DoHighLevelEvent( &theEvent );
  167.             break;
  168.         default:
  169.             /*Gripe("\pGot unknown event type" );*/
  170.             break;    
  171.     }
  172.  
  173.     return true;
  174. }
  175.  
  176. Boolean DoNullEvent( EventRecord *theEventPtr )
  177. {
  178.     /* One could execute some sort of background task here */
  179.  
  180.     return true;
  181. }
  182.  
  183. Boolean DoMouseDown( EventRecord *eventPtr )
  184. {
  185.     WindowPtr        theWindow;
  186.     Boolean            result = true;
  187.     Rect            dragRect;
  188.     TEHandle        textH;
  189.     short            partCode;
  190.     ControlHandle    ctlHdl;
  191.     short            curFile;
  192.     short            deskPart;
  193.     Boolean            shifted;
  194.     
  195.     deskPart = FindWindow( eventPtr->where, &theWindow );
  196.     switch( deskPart ){
  197.         case inDesk:
  198.             break;
  199.         case inMenuBar:
  200.             FixMenuMarks();
  201.             curFile = CurResFile();
  202.             UseResFile( gPrefFileRefNum );        /* Make SICN resource accessible */
  203.             result = DoMenuCommand( MenuSelect( eventPtr->where ) );
  204.             UseResFile( curFile );
  205.             FixMenuMarks();
  206.             break;
  207.         case inSysWindow:
  208.             SystemClick( eventPtr, theWindow );
  209.             break;
  210.         case inContent:
  211.             if ( theWindow != FrontWindow() ){
  212.                 SelectWindow( theWindow );
  213.             } else {
  214.                 GlobalToLocal( &(eventPtr->where) );
  215.                 partCode = FindControl( eventPtr->where, theWindow, &ctlHdl );
  216.                 if ( partCode == 0 ){
  217.                     textH = (TEHandle)GetWRefCon( theWindow );
  218.                     
  219.                     gScrollWindow = theWindow;        /* For use by TrackContentClick */
  220.  
  221.                     /* 1.1.1 MDC We use an intermediate variable rather than just passing
  222.                      * eventPtr->modifiers & shiftKey because this is a two-byte
  223.                      * value, with a bit set in the high byte.  Since a boolean
  224.                      * is a single byte, this resulted in always passing 0, so we
  225.                      * didn't see the shift key.
  226.                      */
  227.  
  228.                     if ( eventPtr->modifiers & shiftKey ){
  229.                         shifted = true;
  230.                     }else{
  231.                         shifted = false;
  232.                     }
  233.                 
  234.                     TEClick( eventPtr->where, 
  235.                             shifted, 
  236.                             textH );
  237.                 }else{
  238.                     DoControl( theWindow, ctlHdl, partCode, eventPtr->where );
  239.                 }
  240.             }
  241.             break;
  242.         case inDrag:
  243.             DragWindow( theWindow, eventPtr->where, &qd.screenBits.bounds );    /* NOT correct but works */
  244.             break;
  245.         case inGrow:
  246.             GrowDocWindow( theWindow, eventPtr->where );
  247.             break;
  248.         case inGoAway:
  249.             if ( TrackGoAway( theWindow, eventPtr->where ))
  250.                 DoCloseWindow();
  251.             break;
  252.         case inZoomIn:
  253.         case inZoomOut:
  254.             if ( TrackBox( theWindow, eventPtr->where, deskPart ) )
  255.                 DoZoom( theWindow, deskPart );
  256.             break;
  257.     }
  258.     return result;
  259. }
  260.  
  261. Boolean DoMenuCommand( long menuSpot )
  262. {
  263.     short theMenu;
  264.     short theItem;
  265.     Boolean result;
  266.         
  267.     theMenu = HiWord( menuSpot );
  268.     
  269.     theItem = LoWord( menuSpot );
  270.     
  271.     switch ( theMenu ){
  272.         case 0:
  273.             result = true;
  274.             break;
  275.         case kAppleMenuID:
  276.             result = DoAppleMenu( theItem );
  277.             break;
  278.         case kFileMenuID:
  279.             result = DoFileMenu( theItem );
  280.             break;
  281.         case kEditMenuID:
  282.             result = DoEditMenu( theItem );
  283.             break;
  284.         case kFontMenuID:
  285.             result = DoFontMenu( theItem );
  286.             break;
  287.         case kStyleMenuID:
  288.             result = DoStyleMenu( theItem );
  289.             break;
  290.         case kServMenuID:
  291.             result = DoServMenu( theItem );
  292.             break;
  293.         case kRegMenuID:
  294.             result = DoRegMenu( theItem );
  295.             break;
  296.     }
  297.     
  298.     HiliteMenu( 0 );
  299.     return result;
  300. }
  301.  
  302. Boolean DoAppleMenu( short theItem )
  303. {
  304.     Handle appleHandle;
  305.     Str255 itemName;
  306.     
  307.     switch ( theItem ){
  308.         case kAMAboutMe:
  309.             DoAboutMe();
  310.             break;
  311.         case kAMDash:
  312.             break;
  313.         default:
  314.             appleHandle = GetResource( 'MENU', kAppleMenuID );
  315.             if ( !appleHandle ){
  316.                 Gripe( "\pCannot get handle to apple menu" );
  317.                 return false;
  318.             }
  319.             
  320.             GetItem( (MenuHandle) appleHandle, theItem, itemName );
  321.             OpenDeskAcc( itemName );
  322.             if ( gDocWindow ){
  323.                 SetPort( gDocWindow );
  324.             }
  325.             break;
  326.     }
  327.     return true;
  328. }
  329.  
  330. Boolean DoFileMenu( short theItem )
  331. {
  332.     switch ( theItem ){
  333.         case kFMNew:
  334.             if ( gDocWindow )
  335.                 return true;
  336.             
  337.             return MakeNewWindow() == noErr;
  338.             break;
  339.         case kFMOpen:
  340.             DoOpenDialog();
  341.             break;
  342.         case kFMClose:
  343.             DoCloseWindow();
  344.             break;
  345.         case kFMSave:
  346.             DoSave();
  347.             break;
  348.         case kFMSaveAs:
  349.             DoSaveAs();
  350.             break;
  351.         case kFMPageSetup:
  352.             DoPageSetup();
  353.             break;
  354.         case kFMPrint:
  355.             DoPrint();
  356.             break;
  357.         case kFMQuit:
  358.             DoQuit();
  359.             break;
  360.     }
  361.     
  362.     return true;
  363. }
  364.  
  365. Boolean DoEditMenu( short theItem )
  366. {
  367.     TEHandle textH;
  368.  
  369.     /* 2.0 MDC added check for read-only documents */
  370.  
  371.     if ( !SystemEdit( theItem - 1 ) ){
  372.         if ( gDocWindow ){
  373.             textH = (TEHandle)GetWRefCon( gDocWindow );
  374.             
  375.             switch( theItem ){
  376.                 case kEMCut:
  377.                     if ( !gReadOnly ){
  378.                         gDocDirty = true;
  379.                         TECut( textH );
  380.                     }else{
  381.                         TECopy( textH );
  382.                     }
  383.                     break;
  384.                 case kEMCopy:
  385.                     TECopy( textH );
  386.                     break;
  387.                 case kEMPaste:
  388.                     if ( !gReadOnly ){
  389.                         gDocDirty = true;
  390.                         TEStylPaste( textH );
  391.                     }
  392.                     break;
  393.                 case kEMClear:
  394.                     if ( !gReadOnly ){
  395.                         gDocDirty = true;
  396.                         TEDelete( textH );
  397.                     }
  398.                     break;
  399.                 case kEMSelectAll:
  400.                     TESetSelect( 0, 32767, textH );
  401.                     break;
  402.                 case kEMOptions:
  403.                     OptionsDialog();
  404.                     break;
  405.             }
  406.             
  407.             SetVertScroll( gDocWindow, gVertScroll );
  408.         }else{
  409.             /* Handle the operations that we can do while the window is closed */
  410.             switch( theItem ){
  411.                 case kEMOptions:
  412.                     OptionsDialog();
  413.                     break;
  414.             }
  415.         
  416.         }
  417.     }
  418.     return true;
  419. }
  420.  
  421. Boolean DoServMenu( short theItem )
  422. {
  423.     OSErr err;
  424.  
  425.     switch( theItem ){
  426. #ifdef NEVER                /* This will return in a future version */
  427.         case kSMCheckSel:
  428.             ToggleSelectCheck();
  429.             break;
  430. #endif
  431.         case kSMNewBatch:
  432.             err = GetNewBatchService();
  433.             if ( err )
  434.                 Gripe( "\pGetNewBatchService failed" );
  435.             break;
  436. #ifdef NEVER                /* This will return in a future version */
  437.         case kSMNewInteractive:
  438.             if ( !gDocWindow )
  439.                 return true;
  440.             break;
  441. #endif
  442.         case kSMRemoveService:
  443.             RemoveServices();
  444.             break;
  445. #ifdef NEVER                /* This will return in a future version */
  446.         case kSMCheckWord:
  447.             if ( !gDocWindow )
  448.                 return true;
  449.             break;
  450. #endif
  451.         case kSMDash:
  452.             break;
  453.         default:
  454.             /* Here is where we actually do some spellchecking! */
  455.             if ( !gDocWindow )
  456.                 return true;
  457.             
  458.             DoSpellCheck( theItem - kSMDash );
  459.             
  460.             break;
  461.     }
  462.     return true;
  463. }
  464.     
  465. Boolean DoMouseUp( void )
  466. {
  467.     return true;
  468. }
  469.  
  470. Boolean DoKeyDown( long message, short modifiers )
  471. {
  472.     char        theChar;
  473.     Boolean        result;
  474.     
  475.     theChar = message & charCodeMask;
  476.     
  477.     if ( modifiers & cmdKey ){
  478.         FixMenuMarks();        /* We need to have the dis/enabling up to date */
  479.         result = DoMenuCommand( MenuKey( theChar ) );
  480.     }else {
  481.     
  482.         /* 2.0 MDC don't allow typing in a readonly document */
  483.  
  484.         if ( gDocWindow && ( gDocWindow == FrontWindow() )){
  485.             if ( !gReadOnly  )
  486.                 gDocDirty = true;
  487.             MyTEKey( gDocWindow, gVertScroll, theChar, gReadOnly  );
  488.         }
  489.         result = true;
  490.     }
  491.         
  492.     return result;
  493. }
  494.  
  495. Boolean DoUpdateEvt( long message )
  496. {
  497.     GrafPtr    curPort;
  498.     
  499.     BeginUpdate( (WindowPtr)message );
  500.     
  501.     if ( (WindowPtr)message == gDocWindow ){
  502.         GetPort( &curPort );
  503.         SetPort( gDocWindow );
  504.         EraseRect( &qd.thePort->portRect );
  505.         TEUpdate( &qd.thePort->portRect, (TEHandle)GetWRefCon( (WindowPtr)message ) );
  506.         
  507.         ShowPictures( gDocWindow, gNumPictures, gResRefNum );
  508.  
  509.         MyDrawGrowIcon( (WindowPtr)message );
  510.         DrawControls( gDocWindow );
  511.         SetPort( curPort );
  512.     }
  513.  
  514.     EndUpdate( (WindowPtr)message );
  515.     return true;
  516. }
  517.  
  518. void MyDrawGrowIcon( WindowPtr theWindow )
  519. {
  520.     RgnHandle    clipRgn;
  521.     Rect        newClip;
  522.     
  523.     /* This will fudge the DrawGrowIcon routine so we only draw the outline for the
  524.      * vertical scroll bar.  It's easier than writing a custom WDEF.
  525.      * We have to set a new clip region because the update region will mask off some
  526.      * of what we might want to draw.
  527.      *
  528.      * thePort must be the window that is getting the icon drawn in it.
  529.      */
  530.  
  531.     clipRgn = NewRgn();
  532.     if ( !clipRgn )
  533.         return;
  534.     
  535.     GetClip( clipRgn );
  536.     
  537.     newClip.top = theWindow->portRect.top;
  538.     newClip.right = theWindow->portRect.right;
  539.     newClip.bottom = theWindow->portRect.bottom;
  540.     newClip.left = newClip.right - 15;
  541.     
  542.     ClipRect( &newClip );
  543.     
  544.     DrawGrowIcon( theWindow );
  545.  
  546.     SetClip( clipRgn );
  547.     
  548.     DisposeRgn( clipRgn );            /* 1.1.1 MDC fix a leak */
  549.     
  550.     return;
  551. }
  552.  
  553. Boolean DoDiskEvt( void )
  554. {
  555.     return true;
  556. }
  557.  
  558. Boolean DoActivateEvt( short modifiers )
  559. {
  560.  
  561.     if ( gDocWindow ){
  562.  
  563.         if ( modifiers & activeFlag ){
  564.             TEActivate( (TEHandle)GetWRefCon( gDocWindow ) );
  565.         } else {
  566.             TEDeactivate( (TEHandle)GetWRefCon( gDocWindow ) );
  567.         }
  568.     }
  569.     return true;
  570. }
  571.  
  572. void GrowDocWindow( WindowPtr theWindow, Point where )
  573. {
  574.     long    newSize;
  575.     short    newWidth;
  576.     short    newHeight;
  577.     GrafPtr    curPort;
  578.     Rect    sizeRect;
  579.     
  580.     SetRect( &sizeRect, 80, 80, 32767, 32767 );
  581.  
  582.     newSize = GrowWindow( theWindow, where, &sizeRect );
  583.     if ( newSize == 0L )
  584.         return;
  585.  
  586.     newWidth = (short) newSize;
  587.     newHeight = (short) ( newSize >> 16 );
  588.     
  589.     GetPort( &curPort );
  590.     SetPort( theWindow );
  591.  
  592.     SizeWindow( theWindow, newWidth, newHeight, true );
  593.  
  594.     ResetWindowParts( theWindow );
  595.  
  596.     SetPort( curPort );
  597.  
  598.     return;
  599. }
  600.  
  601. void ResetWindowParts( WindowPtr theWindow )
  602. {
  603.     GrafPtr    curPort;
  604.  
  605.     /* Resize all of the components of the document window after the window itself
  606.      * has been resized.
  607.      */
  608.  
  609.     GetPort( &curPort );
  610.     SetPort( theWindow );
  611.  
  612.     SetTESize( theWindow );
  613.  
  614.     /* We have to redraw the whole window, since the grow icon, scroll bars, and
  615.      * text rectange have all changed
  616.      */
  617.     
  618.     InvalRect( &( theWindow->portRect ) );
  619.     
  620.     SizeVertScroll();
  621.     SetVertScroll( theWindow, gVertScroll );
  622.     
  623.     SetPort( curPort );
  624.  
  625.     return;
  626. }
  627.  
  628. Boolean DoOSEvt( EventRecord *eventPtr )
  629. {
  630.     Boolean inBackground;
  631.     
  632.     switch ((eventPtr->message >> 24) & 0x0FF) {     /* high byte of message */
  633.         case suspendResumeMessage:              /* suspend/resume is also an activate/deactivate */
  634.             if ( gDocWindow ){
  635.  
  636.                 inBackground = (eventPtr->message & resumeFlag) == 0;
  637.                 if (inBackground) {
  638. #ifdef NEVER        /* Don't use for styled textedit */
  639.                     ZeroScrap();
  640.                     TEToScrap();
  641. #endif
  642.                     if ( gDocWindow ){
  643.                         TEDeactivate( (TEHandle)GetWRefCon( gDocWindow ) );
  644.                     }
  645.                 } else {
  646. #ifdef NEVER        /* Don't use for styled textedit */
  647.                     TEFromScrap();
  648. #endif
  649.                     if ( gDocWindow ){
  650.                         TEActivate( (TEHandle)GetWRefCon( gDocWindow ) );
  651.                     }
  652.                 }
  653.             }
  654.             
  655.         default:
  656.             break;
  657.     }
  658.     return true;
  659. }
  660.  
  661. Boolean DoHighLevelEvent( EventRecord *theEventPtr )
  662. {
  663.     OSErr err;
  664.     
  665.     err = AEProcessAppleEvent( theEventPtr );
  666.  
  667.     if ( err ){
  668.         return true;            /* An error result is OK... */
  669.     }
  670.  
  671.     return true;
  672. }
  673.  
  674. void SetTESize( WindowPtr theWindow )
  675. {
  676.     Rect    txRect;
  677.     TEHandle    hTE;
  678.     short    rectOffset;
  679.     
  680.     GetTERect( &( theWindow->portRect ), &txRect );
  681.     
  682.     hTE = (TEHandle)GetWRefCon( theWindow );
  683.     
  684.     rectOffset = (*hTE)->destRect.top - (*hTE)->viewRect.top;
  685.  
  686.     (*hTE)->viewRect = txRect;
  687.  
  688.     txRect.top += rectOffset;
  689.     txRect.bottom += rectOffset;
  690.  
  691.     (*hTE)->destRect = txRect;
  692.  
  693.     TECalText( hTE );
  694.  
  695.     return;
  696. }
  697.  
  698. void GetTERect( Rect *portRectPtr, Rect *txRectPtr )
  699. {
  700.     /* Return the rect for the textedit field that will fit in the
  701.      * given portRect, allowing for the scroll bar, and a little bit
  702.      * of margin around all the edges.
  703.      */
  704.  
  705.     *txRectPtr = *portRectPtr;
  706.  
  707.     InsetRect( txRectPtr, kTextInset, kTextInset );
  708.     
  709.     txRectPtr->right -= 16;
  710.  
  711.     return;
  712. }
  713.  
  714. void DoQuit( void )
  715. {
  716.     Boolean    reallyQuit;
  717.  
  718.     if ( gDocWindow )
  719.         reallyQuit = DoCloseWindow();
  720.  
  721.     if ( reallyQuit )
  722.         ExitToShell();
  723.     
  724.     return;
  725. }
  726.  
  727. void DoZoom( WindowPtr theWindow, short deskPart )
  728. {
  729.     Boolean isFront;
  730.     
  731.     if ( FrontWindow() == theWindow )
  732.         isFront = true;
  733.     else
  734.         isFront = false;
  735.  
  736.     ZoomWindow( theWindow, deskPart, isFront );
  737.     
  738.     ResetWindowParts( theWindow );
  739.  
  740.     return;
  741. }
  742.  
  743.